//
//  BIDOwnerPersonalRepairListViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/7.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDOwnerPersonalRepairListViewController.h"
#import "BIDOwnerPersonalRepairDetailViewController.h"
#import "BIDPersonalRepairViewController.h"
#import "BIDOwnerPersonalRepairCell.h"

static NSString *personalRepairListURL = @"client/repair/ownRepair.action";

@interface BIDOwnerPersonalRepairListViewController ()<BIDBaseListViewDelegate>
{
    UISegmentedControl *_segmentedCtrl;
    //总的数据源
    NSMutableArray *_totalDataSource;
    //
    NSUInteger _curSelectIndex;
    //报修状态
    NSString *_repairStatus;
    BOOL _bRegister;
}
@end

@implementation BIDOwnerPersonalRepairListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"个人报修";
    _curSelectIndex = 0;
    _repairStatus = @"";
    NSArray *tempArr = @[@{@"page":@0, @"dataSource":@[], @"repairStatus":@""},
                         @{@"page":@0, @"dataSource":@[], @"repairStatus":@"0"},
                         @{@"page":@0, @"dataSource":@[], @"repairStatus":@"1"},
                         @{@"page":@0, @"dataSource":@[], @"repairStatus":@"2"}];
    _totalDataSource = [NSMutableArray array];
    [_totalDataSource addObjectsFromArray:tempArr];
    //
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPersonalRepairHandler)];
    //
    NSArray *titles = @[@"全部", @"待处理", @"待确认", @"待评价"];
    _segmentedCtrl = [[UISegmentedControl alloc] initWithItems:titles];
    _segmentedCtrl.tintColor = RGBACOLOR(27.f, 211.f, 205.f, 1.f);
    [_segmentedCtrl addTarget:self action:@selector(segmentedCtrlValueChangedHandler:) forControlEvents:UIControlEventValueChanged];
    _segmentedCtrl.selectedSegmentIndex = 0;
    //
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGRect segmentedCtrlFrame = _segmentedCtrl.frame;
    segmentedCtrlFrame.size.width = screenSize.width;
    segmentedCtrlFrame.origin.y = 0;
    _segmentedCtrl.frame = segmentedCtrlFrame;
    //
    CGRect tableViewFrame = self.myTableView.frame;
    tableViewFrame.origin.y = CGRectGetMaxY(segmentedCtrlFrame);
    tableViewFrame.size.height = screenSize.height-64-CGRectGetHeight(segmentedCtrlFrame);
    self.myTableView.frame = tableViewFrame;
    [self.view addSubview:_segmentedCtrl];
    //
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    self.curPage = 0;
    [self.dataSourceArr removeAllObjects];
    [self loadData];
}

- (void)loadData
{
    //
    self.curPage++;
    //
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getPayServerAddr], personalRepairListURL];
    NSString *strPost = @"";
    if(_repairStatus.length == 0)
    {
        strPost = [[NSString alloc] initWithFormat:@"id=%@&status=%@&page=%ld&rows=%ld", [AppDelegate getUserId], _repairStatus, self.curPage, self.countPerPage];
    }
    else
    {
        strPost = [[NSString alloc] initWithFormat:@"id=%@&status=%@&page=%ld&rows=%ld", [AppDelegate getUserId], _repairStatus, self.curPage, self.countPerPage];
    }
    NSLog(@"self.curPage=%ld", self.curPage);
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            NSArray *arr = responseDic[@"rows"];
            [self.dataSourceArr addObjectsFromArray:arr];
            if(self.dataSourceArr.count < self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            else
            {
                [self.myTableView hasMoreData];
            }
            [self.myTableView reloadData];
        }
    }];
}

- (void)addPersonalRepairHandler
{
    BIDPersonalRepairViewController *vc = [[BIDPersonalRepairViewController alloc] initWithNibName:@"BIDPersonalRepairViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)segmentedCtrlValueChangedHandler:(UISegmentedControl *)segmentedCtrl
{
    NSNumber *pageValue = [NSNumber numberWithUnsignedInteger:self.curPage];
    NSDictionary *tempDic = @{@"page":pageValue, @"dataSource":self.dataSourceArr, @"repairStatus":_repairStatus};
    [_totalDataSource replaceObjectAtIndex:_curSelectIndex withObject:tempDic];
    //
    NSUInteger index = segmentedCtrl.selectedSegmentIndex;
    _curSelectIndex = index;
    //
    tempDic = _totalDataSource[index];
    _repairStatus = tempDic[@"repairStatus"];
    pageValue = tempDic[@"page"];
    self.curPage = [pageValue integerValue];
    self.dataSourceArr = [[NSMutableArray alloc] initWithArray:tempDic[@"dataSource"]];
    if(self.dataSourceArr.count == 0)
    {
        [self loadData];
    }
    else
    {
        [self.myTableView reloadData];
    }
}


#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    if(!_bRegister)
    {
        _bRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDOwnerPersonalRepairCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDOwnerPersonalRepairCell *repairCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dic = self.dataSourceArr[row];
    repairCell.faultTypeLabel.text = [[NSString alloc] initWithFormat:@"故障类型：%@", dic[@"repairTypeName"]];
    repairCell.timeLabel.text = [[NSString alloc] initWithFormat:@"报修日期：%@", dic[@"createTime"]];
    repairCell.houseNameLabel.text = dic[@"repairPlace"];
    NSNumber *repairStatusValue = dic[@"status"];
    NSString *strRepairStatus = @"";
    switch([repairStatusValue integerValue])
    {
        case 0:
        {
            strRepairStatus = @"待处理";
        }
            break;
        case 1:
        {
            strRepairStatus = @"待确认";
        }
            break;
        case 2:
        {
            strRepairStatus = @"待评价";
        }
            break;
        case 3:
        {
            strRepairStatus = @"已评价";
        }
            break;
        case 6:
        {
            strRepairStatus = @"已撤单";
        }
            break;
    }
    repairCell.statusLabel.text = strRepairStatus;
    cell = repairCell;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 99.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = self.dataSourceArr[indexPath.row];
    BIDOwnerPersonalRepairDetailViewController *vc = [[BIDOwnerPersonalRepairDetailViewController alloc] init];
    vc.repairID = dic[@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
